// pages/orderDetail/orderDetail.js

//获取app实例
const app = getApp();
var utils = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: null,
    item_info: [],
    cancelOrderReason: [
      '订单信息填写错误',
      '错拍/多拍/不想要',
      '未按要定时间发货',
      '其他原因'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init(options.orderId);
    this.setData({ detail: options.info})
    options.aftersale && this.setData({ aftersale: true});
  },

  //初始化我的订单
  init(userId, cb) {
    console.log(this.data)
    wx.request({
      url: app.baseURL + 'Order&a=read_order',
      data: {
        user_id: app.globalData.userId,
        order_id: this.options.orderId
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.updateData(res.data);
          this.setData({
            data: res.data
          })
          typeof cb == 'function' && cb();
        }
      }
    })
  },

  //更新数据函数
  updateData(data) {
    const orderData = data.order_info;
    orderData.addTime = utils.formatTime(orderData.add_time * 1000); 
    this.setData({
      info: orderData,
      item_info: data.item_info
    })
  },
  // 取消订单
  CancellationOfOrder(e) {
    console.log(e)
    const orderId = this.options.orderId;
    const orderStatus = e.currentTarget.dataset.status;
    wx.request({
      url: app.baseURL + 'Order&a=del_order',
      data: {
        user_id: app.globalData.userId,
        order_id: orderId,
        status: orderStatus,
        type: 1
      },
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data.status == 2) {
            wx.hideLoading();
            //前往结果页面
            wx.reLaunch({
              url: "/pages/commentResult/commentResult?title=操作成功&tip=您已成功取消订单"
            }) 
          } else if (res.data.status == 3) {
            wx.hideLoading();
            wx.showToast({
              title: '取消失败，请重试', icon: 'none'
            })
          }
        }  
      },
      fail: (res) => {
        wx.hideLoading();
        wx.showToast({ title: '网络错误', icon: 'none' })
      }
    })
  },
  // 删除订单
  clicktoDelete(e) {
    const orderId = this.options.orderId;
    const orderStatus = e.currentTarget.dataset.status;
    wx.showModal({
      title: '温馨提示',
      content: '确定删除当前商品吗？',
      confirmColor: "#f00",
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '加载中' });
          wx.request({
            url: app.baseURL + 'Order&a=del_order',
            data: {
              user_id: app.globalData.userId,
              order_id: orderId,
              status: orderStatus,
              type: 2
            },
            success: (res) => {
              if (res.statusCode == 200) {
                if (res.data.status == 4) {
                  //前往结果页面
                  wx.reLaunch({ url: "/pages/commentResult/commentResult?title=删除成功&tip=您已成功删除此订单" });
                } else {
                  wx.hideLoading();
                  wx.showToast({ title: '删除失败，请重试', icon: 'none' })                
                }
              }  
            },
            fail: (res) => {
              wx.hideLoading();
              wx.showToast({ title: '网络错误', icon: 'none' })
            }
          });
        }
      }
    })
  },
  // 确认收货
  confirmReceipt(e) {
    let order_id = e.currentTarget.dataset.order_id;
    wx.showModal({
      title: '确认收到货了吗？',
      content: '为保障您的售后权益,请收到货确认无误后,再确认收货哦！',
      confirmText: "确认收货",
      confirmColor: "#f00",
      success: (res) => {
        if (res.confirm) {
          //加载中提示
          wx.showLoading({ title: '加载中' });
          wx.request({
            url: app.baseURL + 'Order&a=getted',
            data: {
              user_id: app.globalData.userId,
              order_id: order_id
            },
            success: (res) => {
              if (res.statusCode == 200) {
                if (res.data.status == 1) {
                  //收货成功
                  wx.hideLoading();
                  //前往结果页面
                  wx.reLaunch({ url: "/pages/commentResult/commentResult?title=收货成功&tip=您已成功收货" });
                } else if (res.data.status == 2) {
                  //收货失败
                  wx.hideLoading();
                  wx.showToast({ title: '操作失败，请重试', icon: 'none' })
                }
              }
            },
            fail: (res) => {
              wx.hideLoading();
              wx.showToast({ title: '网络错误', icon: 'none' })
            }
          })
        }
      }
    })
  },

  //立即支付
  toOrderPay() {
    let result = {
      id: this.data.data.order_info.id, //订单id
      order_number: this.data.data.order_info.order_number, //订单号
      realprices: this.data.data.order_info.realprices, //订单价格
      total_nums: this.data.data.order_info.total_nums //商品数量
    };
    const result_json = JSON.stringify(result);
    console.log(result_json)
    wx.navigateTo({
      url: '/pages/orderPay/orderPay?result=' + result_json
    })
  },

})